
"""
  绘制lstm模型预测结果
"""


# !处理路径导入问题（添加绝对路径）！！！
import sys
import os
CODE_INTERNAL_PATH = os.path.abspath(os.path.join(os.path.dirname(__file__), '../../')) # 生成Code文件夹内部对应的绝对路径
sys.path.append(CODE_INTERNAL_PATH)

# 导入内部包
from utils.read_data import read_lstm_verify_data
from utils.draw_data import draw_line_chart

FILE_PATH_I80_1_to = "../../../Data/Ngsim数据集/I80数据集/4. 验证数据/2. 换道数据/trajectories-0400-0415_transformer_lanechange.txt" # 

def extract_test_data(test_data):
  # 真实数据
  true_acc = [row[0] for row in test_data]
  # 预测数据
  pred_acc = [row[1] for row in test_data]
  # 均值
  mean_acc = [row[2] for row in test_data]
  # 标准差
  std_acc = [row[3] for row in test_data]
  # 3标准差下限
  lower_bound_3_std_acc = [row[8] for row in test_data]
  # 3标准差上限
  upper_bound_3_std_acc = [row[9] for row in test_data]

  return true_acc, pred_acc, mean_acc, std_acc, lower_bound_3_std_acc, upper_bound_3_std_acc

def draw_chart(true_acc, pred_acc, mean_acc, std_acc, lower_bound_3_std_acc, upper_bound_3_std_acc, ticks=None):
  data_x = [i for i in range(len(true_acc))]

  # 绘制真实数据
  datas_y = [{"data":true_acc, "des":"真实数据"}, {"data":pred_acc, "des":"预测数据"}, {"data":mean_acc, "des":"均值"}, {"data":lower_bound_3_std_acc, "des":"3标准差下限"}, {"data":upper_bound_3_std_acc, "des":"3标准差上限"}]
  labels = {"x":"时间（单位:0.1s）", "y":"acc"}
  draw_line_chart(data_x, datas_y, labels, "lstm模型预测验证", ticks)

def calc_sort_value(item):
  accumulate_diff = 0

  for row in item:
    accumulate_diff += abs(row[0] - row[2])
  
  return accumulate_diff / len(item)

if __name__ == '__main__':
  # 读取数据
  datas = read_lstm_verify_data(FILE_PATH_I80_1_to)

  # 排序数据
  sort_datas = sorted(datas, key=calc_sort_value)

  # 选取一个测试数据
  test_data = sort_datas[30]

  # 提取数据
  true_acc, pred_acc, mean_acc, std_acc, lower_bound_3_std_acc, upper_bound_3_std_acc = extract_test_data(test_data)

  # 绘制数据
  draw_chart(true_acc, pred_acc, mean_acc, std_acc, lower_bound_3_std_acc, upper_bound_3_std_acc, [-0.5, -0.25, 0, 0.25, 0.5])